package com.ziq.zcamp.community

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.ziq.base.extension.launchWithTry
import com.ziq.zcamp.api.request.post.PostFollowListParam
import com.ziq.zcamp.api.request.user.CommunityFollowRecommendListParam
import com.ziq.zcamp.api.request.user.UserNumRelationParam
import com.ziq.zcamp.api.respond.post.PostFollowListRespond
import com.ziq.zcamp.api.respond.user.UserNumRelationRespond
import com.ziq.zcamp.api.respond.user.UserRelationListRespond
import com.ziq.zcamp.api.service.PostingService
import com.ziq.zcamp.api.service.UserAccountService
import com.ziq.zcamp.api.service.request
import com.ziq.zcamp.bean.post.PostingDetail
import com.ziq.zcamp.bean.user.UserRelationListItem
import com.ziq.zcamp.manager.UserManager
import com.ziq.zcamp.view.StatusView
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import javax.inject.Inject

@HiltViewModel
class CommunityFollowViewModel @Inject constructor() : ViewModel() {

    @Inject
    lateinit var mPostingService: PostingService

    @Inject
    lateinit var mUserAccountService: UserAccountService

    val listStatusShow = MutableLiveData<Int>()
    val listRequestFinished = MutableLiveData<Boolean>()
    val listHasMore = MutableLiveData<Boolean>()
    val list = MutableLiveData<MutableList<PostingDetail>>()

    var cursor :String? = null

    val followStatusShow = MutableLiveData<Int>()
    val followList = MutableLiveData<MutableList<UserRelationListItem>>()

    fun onRefresh(showProgress: Boolean = false){
        cursor = null
        if(showProgress){
            listStatusShow.postValue(StatusView.LOADING)
        }
        requestList()
    }

    fun onLoadMore(){
        requestList()
    }

    fun requestList(
        onSuccess: ((result: PostFollowListRespond) -> Unit)? = {
            if (it.data != null){
                val tempList = mutableListOf<PostingDetail>()
                if(cursor != null){
                    tempList.addAll(list.value?: emptyList())
                }
                if(it.data?.list?.isNotEmpty() == true){
                    for (item in it.data!!.list!!){
                        if(!tempList.contains(item)){
                            tempList.add(item)
                        }
                    }
                }
                list.postValue(tempList)

                cursor = it.data?.cursor

                if(it.data?.has_more == true){
                    listHasMore.postValue(true)
                } else {
                    listHasMore.postValue(false)
                }

                if(tempList.isNotEmpty()){
                    listStatusShow.postValue(StatusView.IDLE)
                }else {
                    listStatusShow.postValue(StatusView.NO_DATA)
                }
            }else {
                if(cursor == null){
                    listStatusShow.postValue(StatusView.NET_ERROR)
                } else {
                    listStatusShow.postValue(StatusView.IDLE)
                }
                listHasMore.postValue(false)
            }
            listRequestFinished.postValue(true)
        },
        onFail: ((result: String) -> Unit)? = {
            if(cursor == null){
                listStatusShow.postValue(StatusView.NET_ERROR)
            } else {
                listStatusShow.postValue(StatusView.IDLE)
            }
            listRequestFinished.postValue(true)
        }) {

        launchWithTry(whenTry = {
            val respond = mPostingService.postFollowList(PostFollowListParam().apply {
                this@CommunityFollowViewModel.cursor?.let {
                    this.cursor = it
                }
            }).request()

            if(respond.data?.list?.isNotEmpty() == true){
                for(item in respond.data?.list!!){
                    item.parseData()
                }
            }

            onSuccess?.invoke(respond)
        }, whenCatch = {
            withContext(Dispatchers.Main) {
                onFail?.invoke("$it")
            }
        })
    }

    fun requestFollowList(
        onSuccess: ((result: UserRelationListRespond) -> Unit)? = {
            if (it.users != null){
                val tempList = mutableListOf<UserRelationListItem>()
                if(it.users?.isNotEmpty() == true){
                    tempList.addAll(it.users!!)
                }
                followList.postValue(tempList)
                if(tempList.isNotEmpty()){
                    followStatusShow.postValue(StatusView.IDLE)
                }else {
                    followStatusShow.postValue(StatusView.NO_DATA)
                }
            }else {
                followStatusShow.postValue(StatusView.NET_ERROR)
            }
        },
        onFail: ((result: String) -> Unit)? = {
            followStatusShow.postValue(StatusView.NET_ERROR)
        }) {
        followStatusShow.postValue(StatusView.LOADING)

        launchWithTry(whenTry = {
            val respond = mUserAccountService.communityFollowRecommendList(CommunityFollowRecommendListParam().queryMap).request()
            onSuccess?.invoke(respond)
        }, whenCatch = {
            withContext(Dispatchers.Main) {
                onFail?.invoke("$it")
            }
        })
    }

    fun requestUserNumRelation(
        onSuccess: ((result: UserNumRelationRespond) -> Unit)? = {
        },
        onFail: ((result: String) -> Unit)? = {
        }) {
        launchWithTry(whenTry = {
            val respond = mUserAccountService.userNumRelation(UserNumRelationParam().apply {
                this.id = UserManager.instance.getUid()
            }.queryMap).request()
            onSuccess?.invoke(respond)
        }, whenCatch = {
            withContext(Dispatchers.Main) {
                onFail?.invoke("$it")
            }
        })
    }


}